<?php
class Courses
{
	private $id;
	private $name;
	private $courseHour;
	private $major;

	/**
	 * 一门课拥有一位科任老师
	 * @var Teacher
	 */
	private $teacher;

    // 构造函数在创建对象实例时，自动调用做初始化操作
	public function __construct($id,$name,$courseHour,$major)
	{
		$this->id = $id;
		$this->name = $name;
		$this->courseHour = $courseHour;
		$this->major = $major;
	}

	// 魔术方法，$name为属性名
	public function __get($name)
	{
		if (property_exists(self::class,$name)) {
			return $this->name;
		}
		return null;
	}

	// 魔术方法，$name为属性名 $value为属性值
	public function __set($name,$value)
	{
		if (property_exists(self::class, $name)) {
		    $this->name = $value;
		}
	}

	// 把当前对象转换成字符串形式，档直接输出该类的对象时，自动调用
    public function __tostring()
    {
    	return 'id:' . $this->id . '课程:' . $this->name . '课时:' . $this->courseHour . '专业:' . $this->major . '<br/>' . '任课老师' . $this->teacher->name;
    }
}